/* rank data per translation */

use entropy_bible_constant, clear

merge m:1 translation using data_bible_final, nogenerate keep(3)

bysort trans: egen rank_m=rank(D_structure), field
bysort trans: egen rank_s=rank(D_order), field


gen denom=(rank_m-3.5)*(rank_s-3.5)
gen nomx=(rank_m-3.5)^2
gen nomy=(rank_s-3.5)^2

bysort translation: egen sum_denom=total(denom)
bysort translation: egen sum_nomx=total(nomx)
bysort translation: egen sum_nomy=total(nomy)
gen corr=sum_denom/sqrt(sum_nomx*sum_nomy)

bysort translation: gen id=_n
sum corr if id==1, d 
noisily di %3.2f r(p50)



keep book trans rank*  
sum rank_m if book==40
local N: di %5.0fc r(N)

capture program drop plot_it
program plot_it
args book
qui {
	noisily di `"plotting `book'"'
preserve
if `book`b''==40 {
		local title`b' Matthew
	}
	if `book`b''==41 {
		local title`b' Mark
	}
	if `book`b''==42 {
		local title`b' Luke
	}
	if `book`b''==43 {
		local title`b' John
	}
	if `book`b''==44 {
		local title`b' Acts
	}
	if `book`b''==45 {
		local title`b' Romans
	}
	if `book`b''==46 {
		local title`b' Corithians1
	}
	if `book`b''==47 {
		local title`b' Corithians2
	}	
	if `book`b''==58 {
		local title`b' Hebrews
	}	
	if `book`b''==66 {
		local title`b' Revelation
	}	
	histogram rank_m if book==`book', ///
	discrete start(1) percent nodraw saving(2.gph, replace)  ///
	scheme(s2mono) graphregion(color(white))  ///
	xlabel(1 2 3 4 5 6) ylabel(0 25 50 75,grid gmax) yscale(off) xscale(nofextend) ytitle("") xtitle(`"Word structure"')
	
	histogram rank_s if book==`book', ///
	discrete start(1) percent nodraw saving(1.gph, replace)  ///
	scheme(s2mono) graphregion(color(white))  ///
	xlabel(1 2 3 4 5 6) ylabel(0 25 50 75,grid gmax) yscale(nofextend) xscale(nofextend)  ytitle("") xtitle(`"Word order"')
	
	keep if book==`book'
	local denom=_N
	forvalues rm=1/6 {
		forvalues rs=1/6 {
			count if rank_m==`rm'&rank_s==`rs'
			local count_`rm'_`rs'=r(N)
		}
	}
	
	clear
	set obs 6
	gen mr=""
	forvalues rm=1/6 {
		replace mr="r`rm'" in `rm'
	}
		forvalues rs=1/6 {
			gen r`rs'=.
		}
	forvalues rm=1/6 {
		forvalues rs=1/6 {
			replace r`rs'=`count_`rm'_`rs''*100/`denom' if mr=="r`rm'"
		}
	}

	mkmat r*, matrix(mat) rownames(mr)

	plotmatrix, m(mat) freq formatcells(%3.0f) legend(off) c(gray) ylabel(,angle(0))  ///
	saving(3.gph, replace) yscale(alt) nodraw scheme(s2mono) graphregion(color(white)) ///
	ytitle(`"Word structure"') xtitle(`"Word order"') fxsize(34)

	graph combine 1.gph 2.gph, ycommon xcommon nodraw cols(2) xsize(2) ysize(1) saving(12, replace) imargin(0 0 0 0) fxsize(66) scheme(s2mono) graphregion(color(white))


	graph combine 12.gph 3.gph, cols(2) xsize(3) ysize(1) imargin(0 0 0 0) scheme(s2mono) graphregion(ifcolor(white) fcolor(white) lcolor(black)) title("`title'", size(vlarge)) nodraw saving(`book', replace)
}
	end
	
	
	foreach b in 66 43 40 41 42 44 {
		plot_it `b'
	}

graph combine 66.gph 43.gph 40.gph 41.gph 42.gph 44.gph, ///
note("N: `N'") cols(1) xsize(1) ysize(2.2) scheme(s2mono) graphregion(color(white)) iscale(1.05)
graph export fig5.png, height(10000) replace  

foreach b in 1 2 12 3 66 43 40 41 42 44 {
		capture erase `b'.gph
	}
window manage close graph 
exit

